package com.data.algorithm.sort;

/**
 * 选择排序
 *
 * @author wangjie
 * @version V1.0
 * @date 2020/3/6
 */
public class SelectSort {

    public static void main(String[] args) {
        int[] arr = new int[80000];

        for (int i = 0; i < 80000; ++i) {
            arr[i] = (int) (Math.random() * 8000000.0D);
        }

        Long startTime = System.currentTimeMillis();
        System.out.println("开始排序");
        selectSort(arr);
        Long endTime = System.currentTimeMillis() - startTime;

        System.out.println("排序耗时：" + endTime + "毫秒");
        System.out.println("排序耗时：" + endTime / 1000 + "秒");
    }

    /**
     * 选择排序
     * @param arr
     */
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; ++i) {
            int minIndex = i;
            int min = arr[i];

            for (int j = i + 1; j < arr.length; ++j) {
                if (min > arr[j]) {
                    min = arr[j];
                    minIndex = j;
                }
            }

            if (minIndex != i) {
                arr[minIndex] = arr[i];
                arr[i] = min;
            }
        }

    }
}
